package cn.bs.busuo.front.account.controller;

import cn.bs.busuo.front.account.pojo.vo.AllTagVO;
import cn.bs.busuo.front.account.pojo.vo.UserCharacterVO;
import cn.bs.busuo.front.account.service.ITagService;
import cn.bs.busuo.common.pojo.authentication.CurrentPrincipal;
import cn.bs.busuo.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/front/users/tag")
@Api(tags = "2.用户性格标签接口")
public class TagController {
    @Autowired
    private ITagService tagService;

    /**
     * 用户通过该接口添加性格标签
     */
    @PostMapping("/add/self")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户添加性格标签(自身)")
    @ApiOperationSupport(order = 450)
    public JsonResult deleteFriend(@RequestBody List<Long> ids,
                                   @ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal){
        int count = tagService.AddNewTag(principal.getId(),ids);
        return JsonResult.ok("成功添加了"+count+"个性格标签");
    }

    /**
     * 用户通过该接口查询性格标签
     */
    @GetMapping("/list/self")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户查询性格标签(自身)")
    @ApiOperationSupport(order = 460)
    public JsonResult listTagSelf(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal){
        List<UserCharacterVO> userCharacterVOS = tagService.listTag(principal.getId());
        return JsonResult.ok(userCharacterVOS);
    }

    /**
     * 用户通过该接口查询性格标签
     */
    @GetMapping("/{userId:[0-9]+}/list/others")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户查询性格标签别人)")
    @ApiOperationSupport(order = 470)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult listTagOthers(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！")Long userId){
        List<UserCharacterVO> userCharacterVOS = tagService.listTag(userId);
        return JsonResult.ok(userCharacterVOS);
    }

    /**
     * 用户通过该接口删除性格标签
     */
    @PostMapping("/delete/self")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户删除性格标签(自身)")
    @ApiOperationSupport(order = 480)
    public JsonResult deleteTag(@RequestBody List<Long> ids,
                                @ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal){
        int count = tagService.deleteTag(principal.getId(),ids);
        return JsonResult.ok("成功删除了"+count+"个性格标签");
    }

    /**
     * 用户查询所有性格标签
     */
    @GetMapping("/list/all")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户查询所有性格标签")
    @ApiOperationSupport(order = 490)
    public JsonResult listAllTag(){
        List<AllTagVO> allTagVOS = tagService.listAllTag();
        return JsonResult.ok(allTagVOS);
    }
}
